/*
 * This file is part of Smart Route for S60 3rd Edition terminals
 * 
 * Copyright (C) 2006  Harri Salminen

 * This program free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 *(at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
 */
 
#include <aknviewappui.h>
#include <eikmenub.h>
#include <avkon.hrh>
#include <barsread.h>
#include <stringloader.h>
#include <aknlists.h>
#include <eikenv.h>
#include <akniconarray.h>
#include <eikclbd.h>
#include <akncontext.h>
#include <akntitle.h>
#include <eikbtgpc.h>
#include <akncommondialogs.h>
#include <aknnotewrappers.h> 
#include "TripInfo.h"
#include "SmartRoute.rsg"
#include "SmartRoute.hrh"
#include "RouteListBoxView.h"
#include "MainScreen.hrh"
#include "TripInfo.hrh"
#include "RouteListBox.h"

CRouteListBoxView::CRouteListBoxView()
	{
	iRouteListBox = NULL;	
	}

CRouteListBoxView::~CRouteListBoxView()
	{
	delete iRouteListBox;
	}

CRouteListBoxView* CRouteListBoxView::NewL()
	{
	CRouteListBoxView* self = CRouteListBoxView::NewLC();
	CleanupStack::Pop( self );
	return self;
	}

CRouteListBoxView* CRouteListBoxView::NewLC()
	{
	CRouteListBoxView* self = new (ELeave) CRouteListBoxView();
	CleanupStack::PushL( self );
	self->ConstructL();
	return self;
	}

void CRouteListBoxView::ConstructL()
	{
	BaseConstructL( R_ROUTE_LIST_BOX_ROUTE_LIST_BOX_VIEW );
	}
	
TUid CRouteListBoxView::Id() const
	{
	return TUid::Uid( ERouteListBoxViewId );
	}

void CRouteListBoxView::HandleCommandL( TInt aCommand )
	{
	CSmartRouteAppUi* appUi = static_cast<CSmartRouteAppUi*>(AppUi());
    CSmartRouteEngine& engine = appUi->EngineL();
    
	switch(aCommand)
		{
		case EAknSoftkeyBack:
			AppUi()->ActivateLocalViewL(TUid::Uid(EMainScreenViewId));
			break;
		case ESmartRouteOpen:
			{
			TUint32 id = iRouteListBox->SelectedItemId();
			CSmartRoute* route = engine.RestoreRouteL(id);
			CleanupStack::PushL(route);
			CTripInfoForm* routeForm = CTripInfoForm::NewL(); 
    		routeForm->PrepareLC(R_TRIP_INFO_TRIP_INFO);
    		routeForm->SetDataL(route); 
    		routeForm->RunLD();
    		CleanupStack::PopAndDestroy(); //route
			}
			break;
		case ESmartRouteDelete:
			{
			engine.DeleteRouteL(iRouteListBox->SelectedItemId());
			//Redraw route list
			RArray<TShortRoute> points; 
			CleanupClosePushL(points);
			engine.ListRoutesL(points);
			iRouteListBox->AddItemsL(points);
			CleanupStack::PopAndDestroy(); //points
			iRouteListBox->DrawNow();
			}
			break;
		case ESmartRouteExportFile:
			{
			TFileName path;
			HBufC* prompt = CEikonEnv::Static()->AllocReadResourceLC(R_ROUTE_EXPORT_PROMPT_FILENAME);
			TBool accepted = 
				AknCommonDialogs::RunSaveDlgLD(
					path,
					R_EXPORT_MEMORY_SELECTION_DIALOG,
					R_EXPORT_FILE_SELECTION_DIALOG,
					*prompt
				);
			CleanupStack::PopAndDestroy(); //prompt
			if(accepted)
				{
				CSmartRoute* route = engine.RestoreRouteL(iRouteListBox->SelectedItemId());
				CleanupStack::PushL(route);
				CExportRoute* exp = CExportRoute::NewLC(*route);
				TPtr8 exportedRoute = exp->ExportRouteL();
				RFile outFile; 
				TInt status;
				status = outFile.Create(CEikonEnv::Static()->FsSession(), path, EFileWrite|EFileStreamText);
				switch(status)
					{
					case KErrNone:
						User::LeaveIfError(outFile.Write(exportedRoute));
						User::LeaveIfError(outFile.Flush()); 
						outFile.Close();
						HBufC* text = CEikonEnv::Static()->AllocReadResourceLC(R_ROUTE_EXPORT_SUCCESSFULL);
						CAknConfirmationNote* note = new (ELeave) CAknConfirmationNote();
						note->ExecuteLD(*text);
						CleanupStack::PopAndDestroy(); //text
						break;
					case KErrAlreadyExists:
						//Prompt user
						break;
					case KErrPermissionDenied:
						//Alert user
						break;
					default:
						//Alert user
						break;
					}
				CleanupStack::PopAndDestroy(2); //route, exp 
				}
			}
		default:
			AppUi()->HandleCommandL(aCommand);
			break;
		}   
	}

void CRouteListBoxView::DoActivateL(
		const TVwsViewId& aPrevViewId,
		TUid /*aCustomMessageId*/,
		const TDesC8& /*aCustomMessage*/ )
	{
	CSmartRouteAppUi* appUi = static_cast<CSmartRouteAppUi*>(AppUi());
    CSmartRouteEngine& engine = appUi->EngineL();
    
	iPrevViewId = aPrevViewId;
	SetupStatusPaneL();
	
	CEikButtonGroupContainer *cba = AppUi()->Cba();
	if ( cba ) 
		{
		cba->MakeVisible( EFalse );
		}
	
	if ( !iRouteListBox )
		{
		iRouteListBox = CRouteListBox::NewL( ClientRect(), NULL, this );
		iRouteListBox->SetMopParent( this );
		RArray<TShortRoute> points; 
		CleanupClosePushL(points);
		engine.ListRoutesL(points);
		iRouteListBox->AddItemsL(points);
		CleanupStack::PopAndDestroy(); //points
		AppUi()->AddToStackL( *this, iRouteListBox );
		} 
	}

void CRouteListBoxView::DoDeactivate()
	{
	CleanupStatusPane();
	
	CEikButtonGroupContainer *cba = AppUi()->Cba();
	if ( cba ) 
		{
		cba->MakeVisible( ETrue );
		cba->DrawDeferred();
		}
	
	if (iRouteListBox)
		{
		AppUi()->RemoveFromViewStack( *this, iRouteListBox );
		delete iRouteListBox;
		iRouteListBox = NULL;
		}	
	}


void CRouteListBoxView::SetupStatusPaneL()
	{
	TUid contextPaneUid = TUid::Uid( EEikStatusPaneUidContext );
	CEikStatusPaneBase::TPaneCapabilities subPaneContext = 
		StatusPane()->PaneCapabilities( contextPaneUid );
	if ( subPaneContext.IsPresent() && subPaneContext.IsAppOwned() )
		{
		CAknContextPane* context = static_cast< CAknContextPane* > ( 
			StatusPane()->ControlL( contextPaneUid ) );
		context->SetPictureToDefaultL();
		}

	TUid titlePaneUid = TUid::Uid( EEikStatusPaneUidTitle );
	CEikStatusPaneBase::TPaneCapabilities subPaneTitle = 
		StatusPane()->PaneCapabilities( titlePaneUid );
	if ( subPaneTitle.IsPresent() && subPaneTitle.IsAppOwned() )
		{
		CAknTitlePane* title = static_cast< CAknTitlePane* >( 
			StatusPane()->ControlL( titlePaneUid ) );
		TResourceReader reader;
		iEikonEnv->CreateResourceReaderLC( reader, R_ROUTE_LIST_BOX_TITLE_RESOURCE );
		title->SetFromResourceL( reader );
		CleanupStack::PopAndDestroy();
		}
				
	}

void CRouteListBoxView::CleanupStatusPane()
	{
	}

void CRouteListBoxView::HandleStatusPaneSizeChange()
	{
	CAknView::HandleStatusPaneSizeChange();
	TInt result;
	TRAP( result, SetupStatusPaneL() ); 
	}
	
void CRouteListBoxView::DynInitMenuPaneL(TInt aResourceId, CEikMenuPane *aMenuPane)
	{
	if(aResourceId == R_ROUTE_LIST_BOX_MENU_PANE)
		{
		if(iRouteListBox->ListEmpty())
			{
			aMenuPane->SetItemDimmed(ESmartRouteOpen,   ETrue);
			aMenuPane->SetItemDimmed(ESmartRouteExport, ETrue);
			aMenuPane->SetItemDimmed(ESmartRouteDelete, ETrue);
			}
		else
			{
			aMenuPane->SetItemDimmed(ESmartRouteOpen,   EFalse);
			aMenuPane->SetItemDimmed(ESmartRouteExport, EFalse);
			aMenuPane->SetItemDimmed(ESmartRouteDelete, EFalse);
			}
		}
	}
